<template>
  <div class="login-wrap">

    <h2>{{msg}}</h2>

    <template v-if="isDebug">
      <mu-text-field class="accountIdInput" hintText="请输入您的账号" v-model="accountIdInput" /><br/>
      <mu-raised-button label="登录" @click="testLogin" class="login-button" primary/>
    </template>

  </div>

</template>

<style scoped>
  .login-wrap {
    text-align: center;
    margin-top: 50%;
  }

  .accountIdInput {
    margin-top: 24px;
  }

  .login-button {
    font-size: 18px;
    margin-top: 24px;
  }

</style>

<script>
  import util from '../util/util.js'
  import md5 from 'blueimp-md5'
  export default {
    name: 'login',
    data() {
      return {
        params: this.$route.query,
        msg: '请先登录',
        accountIdInput: null,
        isDebug: util.isDebug
      }
    },

    methods: {
      testLogin() {
        let accountIdStr = this.accountIdInput;
        if (accountIdStr == '' || accountIdStr == null) {
          util.toast('账号不能为空');
          return;
        }

        var secretId = "wifiapp"; //测试环境的配置，正式环境会另外给
        var secretKey = "wifiapp2017"; //测试环境的配置，正式环境会另外给
        // var avatar = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1986147978,3326401307&fm=111&gp=0.jpg"; //用户头像
        var avatar = "1"; //用户头像
        var name = "zero-" + accountIdStr; //用户呢称
        var mobile = accountIdStr; //用户手机号，没有则传空字符串
        var ctime = new Date().getTime();
        var str = secretId + accountIdStr + avatar + mobile + name + ctime + secretKey;
        var nonstr = md5(str);

        this.msg = '正在登录 ...';
        let _this = this;
        let pp = {
          accountId: accountIdStr,
          mobile: mobile,
          avatar: avatar,
          name: name,
          ctime: ctime,
          nonstr: nonstr
        }

        this.$http.get('login', {
            params: pp
          })
          .then(function (response) {
            if (response.data.code === '0') {
              util.toast('登录成功');
              //设置用户信息
              if (response.data.data.user) {
                _this.$store.dispatch('SET_MY_INFO', response.data.data.user);
              }
              //设置token
              if (response.data.data.token) {
                let _token = response.data.data.token;
                let tokenStr = 'Bearer ' + _token.openId + ' ' + _token.token;
                _this.$store.dispatch('setToken', tokenStr);
              }

             // 假如已经上传自拍直接跳转到主页
              if (response.data.data.hasAuth === 1) {
                _this.$store.dispatch('setHasAuth', '1');
                _this.$router.replace('/home/arsocial');
              } else {
                _this.$store.dispatch('setHasAuth', '0');
                _this.$router.replace('/selfie');
              }
            }
          }).catch(function (error) {});

      },


      login() {
        this.msg = '正在登录 ...';
        let _this = this;
        let pp = {
          accountId: this.params.accountId,
          mobile: this.params.mobile,
          avatar: this.params.avatar,
          name: this.params.name,
          ctime: this.params.ctime,
          nonstr: this.params.nonstr
        }

        this.$http.get('login', {
            params: pp
          })
          .then(function (response) {
            if (response.data.code === '0') {
              util.toast('登录成功');
              //设置用户信息
              if (response.data.data.user) {
                _this.$store.dispatch('SET_MY_INFO', response.data.data.user);
              }
              //设置token
              if (response.data.data.token) {
                let _token = response.data.data.token;
                let tokenStr = 'Bearer ' + _token.openId + ' ' + _token.token;
                _this.$store.dispatch('setToken', tokenStr);
                _this.$store.dispatch('setThirdId', pp.accountId);
              }

              // 假如已经上传自拍直接跳转到主页
              if (response.data.data.hasAuth === 1) {
                _this.$store.dispatch('setHasAuth', '1');
                _this.$router.replace('/home/arsocial');
              } else {
                _this.$store.dispatch('setHasAuth', '0');
                _this.$router.replace('/selfie');
              }
            }
          }).catch(function (error) {});
      }
    },

    mounted() {
      //先判断是不是要登录
      if (this.params.accountId && this.params.nonstr) {
        //如果是同一个账户
        if (this.$store.state.token && (this.params.accountId === this.$store.state.thirdId)) {
          // 假如已经上传自拍直接跳转到主页
          // localStorage存储进去的都是string类型
          if (this.$store.state.hasAuth === '1') {
            this.$router.replace('/home/arsocial');
          } else {
            this.$router.replace('/selfie');
          }

        } else {
          this.login();
        }

      } else {
        // 如果只是跳转， token 存在就直接登录
        if (this.$store.state.token) {
          // 假如已经上传自拍直接跳转到主页
          if (this.$store.state.hasAuth === '1') {
            this.$router.replace('/home/arsocial');
          } else {
            this.$router.replace('/selfie');
          }
        }
      }
    }
  }

</script>
